// 定义路由
import { createRouter, createWebHistory } from "vue-router";
import login from "@/views/login.vue"
import layout from "@/views/layout.vue";
import student from "@/views/student/index.vue"
import coach from "@/views/coach/index.vue"
import admin from "@/views/admin/index.vue"
import { useTokenStore } from "@/stores/token";
import { ElMessage } from "element-plus";

// 配置路由
const routers_set = [
    { path: "/login", name: "login",component: login },
    {
        path: "/", component: layout,redirect:"student",
        children:[
            {path:"student",component:student},
            {path:"admin",component:admin},
            {path:"coach",component:coach}
        ]
    }
]

const myRouter = createRouter({
    history: createWebHistory(),
    routes: routers_set
})




// 定义路由守卫
myRouter.beforeEach((to, from, next) => {
    const useToken = useTokenStore(); 
    if (!useToken.token) {
        if (to.name === "login") {
            next();
        } else {
            ElMessage.warning("请先登录");
            myRouter.push({ name: 'login' }); 
        }
    } else {
        next();
    }
});

export default myRouter